const app = getApp()
let page = 1;
let page2 = 1;
let page3 = 1;
let page4 = 1;
let pages;
var login_id;
// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_all: [],
    list_dzx: [],
    list_yzx: [],
    list_wtg: [],
    scrollTop: '0rpx',
    scrollHeight: '0rpx',
    scrollTop1: 0,
    scrollHeight1: '0rpx',
    scrollTop2: 0,
    scrollHeight2: '0rpx',
    scrollTop3: 0,
    scrollHeight3: '0rpx',
    scrollTop4: 0,
    scrollHeight4: '0rpx',
    // tab切换 
    currentTab: 0,
  },

  goDetail:res=>{
    wx.navigateTo({
      url: '/pages/detail_yy_sys/detail_yy_sys?yuyueid=' + res.currentTarget.dataset.yuyueid
    })
  },
  scroll1: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop1: event.detail.scrollTop
    });
  },
  scroll2: function (event) {
    console.log(event)
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
     
      scrollTop2: event.detail.scrollTop
    });
  },
  scroll3: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop3: event.detail.scrollTop
    });
  },
  scroll4: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop4: event.detail.scrollTop
    });
  },
  topLoad: function (event) {
    //   该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    page = 1;
    page2 = 1;
    page3 = 1;
    page4 = 1;
    this.getAll(-999);
    this.getAll(0);
    this.getAll(1);
    this.getAll(2);
    this.setData({
      scrollTop: 0,
      scrollTop2: 0,
      scrollTop3: 0,
      scrollTop4: 0,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    console.log(options.cateid)
    login_id = wx.getStorageSync('user') == "" ? -999 : wx.getStorageSync('user').UserID;

    this.setData({
      currentTab: options.cateid
    })


  },

  getAll(YuYueZhuangTai) {
    var that = this;
    wx.showLoading({
      title: '获取数据中',
    })

    let rows = 10;
    // let page = 1;
    wx.request({
      url: app.globalData.apiUrl + 'IS02/HIS020101.ashx?Method=GetYuYueShiYanShiList&loginId='
        + login_id + '&YuYueZhuangTai=' + YuYueZhuangTai + '&rows=' + rows + '&page=' + page ,
      method: 'GET',
      header: {
        'content-type': 'json'
      },
      success: function (datas) {
        wx.hideLoading();
        console.log(datas) 
        if (datas.data.state == 1) {        
          for (var i = 0; i < datas.data.retData.YuYueShiYanShiList.length; i++) {            
            datas.data.retData.YuYueShiYanShiList[i].JieShuRiQi = datas.data.retData.YuYueShiYanShiList[i].JieShuRiQi.slice(5);
            datas.data.retData.YuYueShiYanShiList[i].KaiShiRiQi = datas.data.retData.YuYueShiYanShiList[i].KaiShiRiQi.slice(5);
          }
          console.log(datas.data)
          switch (YuYueZhuangTai) {
            case -999:              
              that.setData({
                list_all: datas.data.retData.YuYueShiYanShiList
              })
              break;
            case 0:
              that.setData({
                list_dzx: datas.data.retData.YuYueShiYanShiList
              })
              break;
            case 1:
              that.setData({
                list_yzx: datas.data.retData.YuYueShiYanShiList
              })
              break;
            case 2:
              that.setData({
                list_wtg: datas.data.retData.YuYueShiYanShiList
              })
              break;
          } 
        } else {
          wx.showToast({
            title: datas.data.resMsg
          })
        }
      }
    })
  },

  // 滑动切换tab 
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  // 点击tab切换 
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    page = 1;
    page2 = 1;
    page3 = 1;
    page4 = 1;
    pages;

    //首页数据
    this.getAll(-999);
    this.getAll(0);
    this.getAll(1);
    this.getAll(2);

    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        that.setData({
          scrollHeight: res.windowHeight - 60,
          scrollHeight1: res.windowHeight - 60,
          scrollHeight2: res.windowHeight - 60,
          scrollHeight3: res.windowHeight - 60,
          scrollHeight4: res.windowHeight - 60,
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  loadMore: function (e) {
    this.getInfo(e);
  },
  loadMore2: function (e) {
    this.getInfo(e);
  },
  loadMore3: function (e) {
    this.getInfo(e);
  },
  loadMore4: function (e) {
    this.getInfo(e);
  },

  getInfo: function (e) {
    var YuYueZhuangTai = e.target.dataset.cid;
    var rows = 10;
    var that = this;
    // 显示加载图标
    // wx.showLoading({
    //   title: '玩命加载中',
    // })
    // 页数+1
    if (YuYueZhuangTai == '-999') {
      page = page + 1;
      pages = page
    } else if (YuYueZhuangTai == '0') {
      page2 = page2 + 1;
      pages = page2 
    } else if (YuYueZhuangTai == '1') {
      page3 = page3 + 1;
      pages = page3
    } else if (YuYueZhuangTai == '2') {
      page4 = page4 + 1;
      pages = page4
    }    
    wx.request({
      url: app.globalData.apiUrl + 'IS02/HIS020101.ashx?Method=GetYuYueYiQiList&loginId='
        + login_id + '&YuYueZhuangTai=' + YuYueZhuangTai + '&rows=' + rows + '&page=' + pages,
      method: "GET",
      // 请求头部
      header: {
        'content-type': 'application/text'
      },
      success: function (datas) {
        if (datas.data.state == 1) {
          wx.hideLoading(); 
          if (datas.data.retData.YuYueYiQiList.length== 0){
            wx.showToast({
              title: '我是有底线的哦！',
              icon: 'none',
              duration: 2000
            })  
          } else{
            for (var i = 0; i < datas.data.retData.YuYueYiQiList.length; i++) {
              datas.data.retData.YuYueYiQiList[i].JieShuShiJian_tmp = datas.data.retData.YuYueYiQiList[i].JieShuShiJian.split(' ');
              datas.data.retData.YuYueYiQiList[i].KaiShiShiJian_tmp = datas.data.retData.YuYueYiQiList[i].KaiShiShiJian.split(' ');
              switch (YuYueZhuangTai) {
                case '-999':
                  var list_all = that.data.list_all;
                  list_all.push(datas.data.retData.YuYueYiQiList[i])
                  that.setData({
                    list_all: list_all
                  })
                  break;
                case '0':
                  var list_dzx = that.data.list_dzx;
                  list_dzx.push(datas.data.retData.YuYueYiQiList[i])
                  that.setData({
                    list_dzx: list_dzx
                  })
                  break;
                case '1':
                  var list_yzx = that.data.list_yzx;
                  list_yzx.push(datas.data.retData.YuYueYiQiList[i])
                  that.setData({
                    list_yzx: list_yzx
                  })
                  break;
                case '2':
                  var list_wtg = that.data.list_wtg;
                  list_wtg.push(datas.data.retData.YuYueYiQiList[i])
                  that.setData({
                    list_wtg: list_wtg
                  })
                  break;
              }
            }             
          }     
        } else {
          // 隐藏加载框
          wx.hideLoading();
          wx.showToast({
            title: '数据异常',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
   
  //下拉刷新  
  onPullDownRefresh: function () {
    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();
    this.getIndex();
    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();
    // 停止下拉动作  
    wx.stopPullDownRefresh();

  },
})